
<template>
	<view>
		<block v-if="dataBuffer">
		<view class="item" 
			v-for="(res, index) in siteList" 
			:key="res.id" 
			@click="backOrder(res)"
		>	
		<view class="delIcon" @click.stop="delAdd(res.id,index)">
			<u-icon name="close-circle-fill" :size="40" color="#ff5500" :index="res.id"></u-icon>
		</view>
			<view class="top" >
				<view class="name">{{ res.username }}</view>
				<view class="phone">{{ res.userphone }}</view>
				<view class="tag">
					<text class="red" v-if="res.status == 1">默认</text>
				</view>
			</view>
			<view class="bottom" >
				{{res.location+res.useraddress}}
				<view class="edit" @click.stop='addressEdit(res)'>
					<u-icon name="edit-pen"  :size="40" color="#999999" :index="res.id" ></u-icon>
				</view>
			</view>
		</view>
		</block>
		<view v-else>
			暂无收货地址信息!
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>

export default {
	data() {
		return {
			siteList: [],//初始化地址数据
			dataBuffer:true
		};
	},
	onShow() {
		this.getAddress()
	},
	methods: {
		// 4.删除地址
		 delAdd(id,index){
			 
			console.log('地址id',id);
			uni.showModal({
				title:'提示',
				content:'确认删除吗?',
				confirmText:'是',
				cancelText:'否',
				success:async (res) => {
					if(res.confirm){
						let result = await this.$http({
							url:'addressremove',
							method:'post',
							header:{
								"content-type":"application/json",
								"authorization":uni.getStorageSync('userInfo').token || null
							},
							data:{
								id:id
							}
						})
						if(result.data.code == 200){
							uni.showToast({
								title:result.data.msg,
								icon:'success'
							})
							this.siteList.splice(index,1)
							if(this.siteList.length <= 0){
								this.dataBuffer = true
							}
							
						}else{
							uni.showToast({
								title:result.data.msg,
								icon:'error'
							})
						}
					}
				}
			})
		},
		// 3.编辑地址
		addressEdit(address){
			let {id,label,location,regioncode,status,uid,useraddress,username,userphone} = address
			uni.navigateTo({
				url:'./addSite?id='+id+'&username='+username+'&useraddress='+useraddress+'&userphone='+userphone+'&location='+location+'&label='+label+'&status='+status
			})
			
		},
		// 2.点击某个地址, 返回订单页面
		backOrder(item){
			console.log(item);
			uni.setStorageSync('addressInfo',item);
			uni.navigateBack({
				delta:1
			})
		},
		// 1.获取地址
		getAddress(){
			// 获取地址列表
			this.$http({
				url:'addresslist',
				header:{
					"content-type":"application/json",
					"authorization":uni.getStorageSync('userInfo').token || null
				},
				data:{
					uid:uni.getStorageSync('userInfo').uid,
				}
			}).then(res => {
				console.log(res);
				if(res.data.code === 200){
					this.dataBuffer = true
					this.siteList = res.data.list
				}else{
					this.dataBuffer = false
				}
			})
		},
		// 跳转到新增地址页面
		toAddSite(){
			uni.navigateTo({
				url:'./addSite'
			})
		}
		
		
	}
};
</script>

<style lang="scss" scoped>
	// 引入样式
	@import  '@/static/css/address.scss';
</style>
